package com.smartum.mpr.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author sergey.valuy
 */
public class SecurityUtil {

	static Logger LOG = Logger.getLogger(SecurityUtil.class.getSimpleName());

	/**
	 * Hash the string
	 * 
	 * @param str path
	 * @return md5 string
	 */
	public static String stringToMD5(String str) {

		String out = null;
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
			digest.update(str.getBytes());
			byte messageDigest[] = digest.digest();
			StringBuffer hexString = new StringBuffer();
			for (byte aMessageDigest : messageDigest) {
				hexString.append(Integer.toHexString(0xFF & aMessageDigest));
			}
			out = hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			LOG.log(Level.WARNING, "Get md5: no such algorithm exception ");
		}
		return out;
	}
}
